package com.he.hsdyc.controller;

import com.he.hsdyc.common.async.BidPulishListener;
import com.he.hsdyc.model.msg.ErrorCode;
import com.he.hsdyc.model.msg.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Api(value="RedisController",description="承接test01",tags={"reids管理"})
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("测试redis")
    @RequestMapping(value = "redis",method = RequestMethod.GET)
    @ApiImplicitParam(name = "mobile", value = "测试手机号", dataType = "string", required = true, paramType = "param")
    public Message testReids(@ApiParam("手机号码") @RequestParam String mobile) {
        String testKey = "TEST:REDIS:"+mobile;
        redisTemplate.opsForValue().set(testKey,"success");
        String template = (String) redisTemplate.opsForValue().get(testKey);
        System.out.println("=======================redis中保存的键：TEST:REDIS:18888888888--》对应的值是："+template);

        //失效监听
        String key = BidPulishListener.constructPublishBid(mobile);
        redisTemplate.opsForValue().set(key,"这是一个定时任务");
        redisTemplate.expire(key,20, TimeUnit.SECONDS);
        System.out.println("=======================当前时间："+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return new Message(ErrorCode.SUCCESS);
    }
}
